#include <gtest/gtest.h>
#include "../mqserver/mq_binding.hpp"

csjmq::BindingManager::ptr bmp;
class Bindingtest : public testing::Environment
{
    virtual void SetUp() override
    {
        bmp = std::make_shared<csjmq::BindingManager>("./data/test.db");
    }

    virtual void TearDown() override
    {
        bmp->clear();
    }
};

TEST(binding_test, bind_test)
{
    bmp->bind("exchange1", "queue1", ".run.music#", true);
    bmp->bind("exchange1", "queue2", ".run.pop#", true);
    bmp->bind("exchange1", "queue3", ".run.tiktok#", true);
    bmp->bind("exchange2", "queue1", ".run.tt1#", true);
    bmp->bind("exchange2", "queue2", ".run.football#", true);
    bmp->bind("exchange2", "queue3", ".run.pingpong#", true);
    ASSERT_EQ(bmp->size(), 6);
}

TEST(binding_test, getbinding_test)
{
    csjmq::Binding::ptr bp = bmp->getBinding("exchange1", "queue3");
    ASSERT_EQ(bp->exchange_name, "exchange1");
    ASSERT_EQ(bp->msgqueue_name, "queue3");
    ASSERT_EQ(bp->binding_key, ".run.tiktok#");
}

TEST(binding_test, unbind_test)
{
    bmp->unbind("exchange1", "queue3");
    csjmq::Binding::ptr bp = bmp->getBinding("exchange1", "queue3");
    ASSERT_EQ(bp.get(), nullptr);
}

TEST(binding_test, removequeuebinding_test)
{
    bmp->removeQueueBindings("queue1");
    ASSERT_EQ(bmp->exists("exchange1", "queue1"), false);
    ASSERT_EQ(bmp->exists("exchange2", "queue1"), false);
    ASSERT_EQ(bmp->exists("exchange2", "queue3"), true);
}

TEST(binding_test, removeExchangebindings_test)
{
    bmp->removeExchangeBindings("exchange2");
    csjmq::Binding::ptr bp = bmp->getBinding("exchange2", "queue3");
    ASSERT_EQ(bp.get(), nullptr);
}

int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new Bindingtest);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
